#include <iostream>
#include <unistd.h>
#include <cstring>
#include <fstream>
#include <signal.h>

#include "Sock.hpp"
#include "Util.hpp"

#define DEFAULTPATH "./wwwroot"
#define HOMEPAGE "/test.html"

class HttpServer
{
public:
    void SendRevcMes(int serverSock)
    {
        Util ut;

        // 接受消息
        std::string buffer;
        ssize_t n = _sock.Revc(serverSock, buffer);
        if (n > 0)
        {
            std::cout << "---------------------------------------" << std::endl;
            std::cout << buffer << std::endl;
            std::cout << "---------------------------------------" << std::endl;
        }

        std::vector<std::string> vline;
        ut.CutString(buffer, "\r\n", &vline);

        std::vector<std::string> vblock;
        ut.CutString(vline[0], " ", &vblock);

        std::string file = vblock[1];
        std::cout << "客户端要访问的文件为: " << file << std::endl;
        if (file == "/")
        {
            file = HOMEPAGE;
        }
        std::string target;
        target += DEFAULTPATH;
        target += file;

        std::cout << "最终访问的文件为: " << target << std::endl;
        std::string content;
        std::ifstream in(target);
        if (in.is_open())
        {
            std::string line;
            while (std::getline(in, line))
            {
                content += line;
            }
            in.close();
        }

        // 回复消息
        std::string Respond;
        if (content.empty())
        {
            // 访问了不存在的文件，跳转到主网站
            Respond += "HTTP/1.1 302 Found\r\n";
            Respond += "Location: http://123.60.181.162:8080/test.html\r\n";
        }
        else
        {
            Respond = "HTTP/1.1 200 OK\r\n";
            Respond += ("Content-Type: text/html\r\n");
            Respond += ("Content-Length: " + std::to_string(content.size()) + "\r\n");
            Respond += "Set-Cookie: 这是一个cookie\r\n";
        }
        Respond += "\r\n";
        Respond += content;

        std::cout << Respond << std::endl;
        // 正文内容
        _sock.Send(serverSock, Respond);
    }

public:
    HttpServer(uint16_t port, const std::string &ip)
        : _listensock(0), _ip(ip), _port(port)
    {
    }

    ~HttpServer()
    {
        close(_listensock);
    }

    void InitServer()
    {
        _listensock = _sock.Socket();
        _sock.Bind(_listensock, _ip, _port);
        _sock.Listen(_listensock);
    }

    void Start()
    {
        signal(SIGCHLD, SIG_IGN);
        while (true)
        {
            std::string clientip;
            uint16_t clientport = 0;
            int serverSock = _sock.Accept(_listensock, &clientip, &clientport);
            if (serverSock < 0)
            {
                continue;
            }
            std::cout << "[clientip: " << clientip << "   clientport: " << clientport << "]" << std::endl;

            int n = fork();
            if (n == 0)
            {
                // child
                close(_listensock);
                // TODO
                SendRevcMes(serverSock);
                close(serverSock);

                exit(0);
            }
            // 接受客户端数据

            close(serverSock);
        }
    }

public:
    int _listensock;
    std::string _ip;
    uint16_t _port;
    Sock _sock;
};
